#coding:utf-8
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from lovewith.supplier.views.modules.supplierapi import SupplierApi
from lovewith.views import site_info
from django_user_agents.utils import get_user_agent
from lovewith.share.views.modules.share import ShareApi
from lovewith.album.views.modules.album import AlbumApi
from lovewith.account.views.modules.userapi import UserApi
from lovewith.subscribe.views.subscribe import get_subscribe_by_user


#判断当前用户浏览的是否是自己的主页
from lovewith.website.views.modules.category import CategoryApi
from lovewith.website.views.modules.color import ColorApi


def validate_user(user, home_id):
    if user and user.id == int(home_id):
        return True
    else:
        return False


#用户订阅的灵感
@login_required
def get_user_subscribe(request, tag, page_no):
    page_no = int(1 and page_no or 1)
    tag = 1 and tag or 'all'

    if tag == 'all':
        share_data = get_subscribe_by_user(request.user, page_no)
    else:
        share_data = ShareApi().get_by_tag(tag, page_no)

    return render_to_response('user/subscribe.jinja', {
        'tab': 'subscribe',
        'is_home': True,
        'current_tag': tag,
        'user_home': request.user,
        'share_data': share_data.get('share'),
        'current_page': page_no,
        'total_page': share_data.get('total'),
    }, context_instance=RequestContext(request, processors=[site_info]))


#用户主页
def show_user_home(request, user_id):
    page_no = int(request.GET.get('page', 1))
    user_data = UserApi.check_data(user_id=user_id)

    if user_data.group_id == 3:
        return HttpResponsePermanentRedirect('/su/%s/' % user_data.id)

    #分享的帖子
    user_share_data = ShareApi().get_post_by_user(user_id, page_no)

    return render_to_response('user/home.jinja', {
        'tab': 'home',
        'is_home': validate_user(request.user, user_id),
        'user_home': user_data,
        'share_data': user_share_data.get('share'),
    }, context_instance=RequestContext(request, processors=[site_info]))


#喜欢的分享
def get_like_share(request, user_id):
    #判断UA
    user_agent = get_user_agent(request)
    page_no = request.GET.get('page', 1)
    user_data = UserApi.check_data(user_id=user_id)

    if user_agent.is_mobile:
        return render_to_response('mobile/list.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'search_title': '喜欢',
            'search_link': '/share/ajax/g/share/?uid=%s&utype=like' % user_id,
            'search_type': 'image',
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        #灵感板数量
        user_like_data = ShareApi.get_like_by_user(user_id, page_no)

        return render_to_response('user/like.jinja', {
            'tab': 'like',
            'is_home': validate_user(request.user, user_id),
            'user_home': user_data,
            'like_data': user_like_data.get('share'),
        }, context_instance=RequestContext(request, processors=[site_info]))


#用户的灵感板
def get_user_album(request, user_id):
    user_data = UserApi.check_data(user_id=user_id)
    album_covers = []
    album_data = AlbumApi.get_album_by_user(user_id)
    for album in album_data:
        #获取前3张图片
        images = []
        album_images_data = AlbumApi.get_cover(album.id)
        for img in album_images_data:
            images.append({
                'path': img.post_image.path
            })

        #图片总数
        images_count = AlbumApi().get_image_count_by_album(album.id)
        album_covers.append({
            'id': album.id,
            'name': album.name,
            'images': images,
            'count': images_count
        })

    supplier_data = SupplierApi.check_data(user_id=int(user_id))

    user_like_data = ShareApi.get_like_by_user(user_id, 1)

    return render_to_response('user/album-all.jinja', {
        'tab': 'album',
        'supplier_data': supplier_data,
        'is_home': validate_user(request.user, user_id),
        'user_home': user_data,
        'like_total': user_like_data.get('total'),
        'like_data': user_like_data.get('share'),
        'album_covers': album_covers,
    }, context_instance=RequestContext(request, processors=[site_info]))


#根据album_id获取灵感列表
def get_album_by_id(request, user_id, album_id, page_no):
    page_no = int(page_no or 1)
    #灵感板
    album_data = AlbumApi.check_data(album_id, user_id)
    #灵感板的图片
    album_share_data = AlbumApi().get_share_by_album(album_id, page_no)
    user_data = UserApi.check_data(user_id=user_id)
    supplier_data = SupplierApi.check_data(user_id=int(user_id))

    return render_to_response('user/album-share.jinja', {
        'tab': 'album',
        'supplier_data': supplier_data,
        'album_data': album_data,
        'album_share_data': album_share_data,
        'user_home': user_data,
        'is_home': validate_user(request.user, user_id)
    }, context_instance=RequestContext(request, processors=[site_info]))